<?php
namespace app\serverintegralmall\logic;
use \think\Db;
use \app\manage\logic\BaseLogic;
use \app\manage\service\ApiBase;

/**
 * @author:      李颖
 * @DateTime:    2017-11-01 10:34:30
 * @Description: 积分商城订单
 */
//积分兑换商品： 形成订单 + 添加提货信息 + 冻结用户积分

class Order extends BaseLogic{
	
    protected $name = 'order';
	protected $config = 'db_config_integralmall';
	protected $pickUpGoodsModel = '\app\serverintegralmall\model\PickUpGoods';
	protected $goodsModel = '\app\serverintegralmall\model\Goods';
	protected $orderModel = '\app\serverintegralmall\model\Order';
    protected $orderLogic = '\app\serverintegralmall\logic\Order';


    /*积分兑换商品*/
    public function integralExchange($data): array 
    {
        //1、积分商品数量-1
        $_descWhere['id'] = $data['goods_id'];
        $_descWhere['status'] = 1;
        $_descWhere['is_del'] = 0;
        $descRes = $this->model($this->goodsModel)->where($_descWhere)->setDec('number', $data['number']);
        if(!$descRes){ return verify_err(null, '减少商品数量失败'); }
    	//2、形成订单记录
    	$goodsInfo = $this->model($this->goodsModel)->where(['id'=>$data['goods_id']])->find();
    	if(!$goodsInfo){ return verify_err(null, '该积分商品不存在'); }
    	$addOrder['user_id'] = $data['user_id'];
    	$addOrder['goods_id'] = $data['goods_id'];
    	$addOrder['number'] = $data['number'];
    	$addOrder['integral'] = $goodsInfo['integral'];
    	$addOrder['order_code'] = $data['order_code'];
    	$addOrder['freeze_status'] = 1;
    	$addOrder['pay_time'] = NOW;
    	$addOrder['create_time'] = NOW;
    	$addOrder['update_time'] = NOW;
    	try{
    		$addOrderRes = $this->model($this->orderModel)->insertGetId($addOrder);
    	}catch(\Exception $e){
    		return server_err(null, $e->getMessage());
    	}
    	//3、添加提货信息
    	$addPick['pick_up_code'] = $data['pick_up_code'];
    	$addPick['order_id'] = $addOrderRes;
    	$addPick['user_id'] = $data['user_id'];
    	$addPick['name'] = $data['name'];
    	$addPick['phone'] = $data['phone'];
    	$addPick['address'] = $data['address'];
    	$addPick['remark'] = $data['remark'];
    	try{
    		$addPickRes = $this->model($this->pickUpGoodsModel)->save($addPick);
    	}catch(\Exception $e){
    		return server_err(null, $e->getMessage());
    	}
    	return auto_return($addOrderRes);
    }

    /*内部获取积分订单列表 分页数据  order左链接goods表 左链接pick_up_goods表*/
    protected function _integralOrderList(array $data = [], array $_where): array 
    {
        $list = Db::connect(config($this->config))
                    ->view('order', '*')
                    ->view('goods', 'title as goods_title', 'goods.id = order.goods_id', 'LEFT')
                    ->view('pick_up_goods', 'pick_up_code,name as pick_up_name,phone as pick_up_phone,address as pick_up_address,remark as pick_up_remark,is_pick,pick_up_time,logistics_company_code,logistics_company_name,waybill_number', 'pick_up_goods.order_id = order.id', 'LEFT')
                    ->where($_where)
                    ->order('order.id DESC')
                    ->paginate(null, false, ['page'=>$data['page'], 'list_rows'=>$data['listRows']])
                    ->toArray();
        if($list['total'] > 0){
            foreach($list['data'] as $k=>&$v){
                if($v['freeze_status'] == 1){
                    if($v['transfer_status'] == 1){
                        $v['status_text'] = $v['is_pick'] == 1 ? '已收货' : '已发货';
                    }else{
                        $v['status_text'] = '待发货';
                    }
                }else{
                    $v['status_text'] = '未知状态';
                }
            }
        }
        return auto_return_page($list);
    }

    /*获取我的积分兑换订单*/
    public function integralOrderList($data): array 
    {
        $_where['order.user_id'] = $data['user_id'];
        $_where['order.is_del'] = 0;
        $data['keywords'] && $_where['goods.title'] = ['like', '%' . $data['keywords'] . '%'];
        return $this->_integralOrderList($data, $_where);
    }

    /*后台获取积分订单列表*/
    public function adminIntegralOrderList($data): array 
    {
        $_where['order.is_del'] = 0;
        $data['keywords'] && $_where['goods.title'] = ['like', '%' . $data['keywords'] . '%'];
        return $this->_integralOrderList($data, $_where);
    }

    /*获取某个积分订单详情*/
    protected function _integralOrderInfo($_where): array 
    {
        $info = Db::connect(config($this->config))
                    ->view('order', '*')
                    ->view('goods', 'title as goods_title, profile_pic as goods_profile_pic', 'goods.id = order.goods_id', 'LEFT')
                    ->view('pick_up_goods', 'pick_up_code,name as pick_up_name,phone as pick_up_phone,address as pick_up_address,remark as pick_up_remark,is_pick,pick_up_time,logistics_company_code,logistics_company_name,waybill_number', 'pick_up_goods.order_id = order.id', 'LEFT')
                    ->where($_where)
                    ->find();
        if(!$info){ return verify_err(null, '未找到该积分订单'); }
        if($info['freeze_status'] == 1){
            if($info['transfer_status'] == 1){
                $info['status_text'] = $info['is_pick'] == 1 ? '已收货' : '已发货';
            }else{
                $info['status_text'] = '待发货';
            }
        }else{
            $info['status_text'] = '未知状态';
        }
        return auto_return($info);
    }

    /*获取我的某个积分订单详情*/
    public function integralOrderInfo($data): array 
    {
        $_where['order.user_id'] = $data['user_id'];
        $_where['order.id'] = $data['id'];
        $_where['order.is_del'] = 0;
        return $this->_integralOrderInfo($_where);
    }
    /*后台获取某个积分订单的详情*/
    public function adminIntegralOrderInfo($data): array 
    {
        $_where['order.id'] = $data['id'];
        $_where['order.is_del'] = 0;
        return $this->_integralOrderInfo($_where);
    }

    /*后台对某个订单进行发货操作(仅修改transfer_status字段)  在这之前先进行操作用户积分 解冻用户积分 + 用户转账平台积分*/
    public function shipmentsGoods($data): array 
    {
        $_where['id'] = $data['id'];
        $_where['is_del'] = 0;
        $_where['freeze_status'] = 1;
        $_where['transfer_status'] = 0;
        $saveData['shipments_time'] = NOW;
        $saveData['transfer_status'] = 1;
        $_pickWhere['order_id'] = $data['id'];
        $_pickWhere['is_pick'] = 0;
        $savePickData['logistics_company_code'] = $data['logistics_company_code'];
        $savePickData['logistics_company_name'] = $data['logistics_company_name'];
        $savePickData['waybill_number'] = $data['waybill_number'];
        $res = $this->model($this->orderModel)->save($saveData, $_where);
        $pickRes = $this->model($this->pickUpGoodsModel)->save($savePickData, $_pickWhere);
        if($res && $pickRes){
            return auto_success($res, '发货成功');
        }
        return auto_failure(null, '发货失败');
    }

    /*我的积分订单 - 确认收货*/
    public function affirmPick($data): array 
    {
        $_where['p.user_id'] = $data['user_id'];
        $_where['p.order_id'] = $data['order_id'];
        $_where['p.is_pick'] = 0;
        $_where['p.status'] = 1;
        $_where['o.transfer_status'] = 1;
        $saveData['p.is_pick'] = 1;
        $saveData['p.pick_up_time'] = NOW;
        $res = $this->model($this->pickUpGoodsModel)
                    ->alias('p')
                    ->join('order o', 'o.id = p.order_id', 'LEFT')
                    ->where($_where)
                    ->update($saveData);
        return auto_return($res, '成功确认收货', '确认收货失败');
    }

    

}